package com.wushi.lenovo.asia5b.app_ui.profile.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.mvp.base.BaseMvpActivity;
import com.mvp.base.model.ResultModel;
import com.mvp.common.GlobalValue;
import com.mvp.utils.ToastUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.CommonConstant;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.other.presenter.CodePresenter;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.VerifyMobileActivityView;
import com.wushi.lenovo.asia5b.app_di.profile.presenter.VerifyMobileActivityPresenter;
import com.wushi.lenovo.asia5b.app_event.ChangeProfileEvent;
import com.wushi.lenovo.asia5b.app_model.profile.CodeModel;
import com.wushi.lenovo.asia5b.app_model.profile.bean.ChangeProfileType;
import com.wushi.lenovo.asia5b.app_widget.ErrorTextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;
import timber.log.Timber;

/**
 * Created by zhangyuncai on 2018/12/8.
 * 修改密码的手机验证界面
 */
public class VerifyMobileActivity extends BaseMvpActivity<
        VerifyMobileActivityView, VerifyMobileActivityPresenter> implements VerifyMobileActivityView {
    @BindView(R.id.et_verifymobile_mobile)
    EditText et_verifymobile_mobile;
    @BindView(R.id.et_verifymobile_code)
    EditText et_verifymobile_code;
    @BindView(R.id.tv_verifymobile_getcode)
    TextView tv_verifymobile_getcode;
    @BindView(R.id.bt_verifymobile_verifycode)
    Button bt_verifymobile_verifycode;
    @BindView(R.id.bt_verifymobile_verifyemail)
    Button bt_verifymobile_verifyemail;
    @BindView(R.id.tv_verifymobile_invalidcode)
    ErrorTextView tv_verifymobile_invalidcode;
    private String resetLoginPassEmail;
    private String resetLoginPassMobile;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_verifymobile;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        resetLoginPassEmail = getIntent().getStringExtra(CommonConstant.RESET_LOGIN_PASS_EMAIL);
        resetLoginPassMobile = getIntent().getStringExtra(CommonConstant.RESET_LOGIN_PASS_MOBILE);

        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {
        et_verifymobile_mobile.setText(resetLoginPassMobile + "");
    }

    @Override
    protected void initData() {
//        sendCode();
    }

    private void sendCode() {
        getPresenter().sendCode(resetLoginPassEmail, CodePresenter.CodeType.RESET_LOGINPASS);
    }

    private void verifyCode() {
        String code = et_verifymobile_code.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            tv_verifymobile_invalidcode.setText(R.string.filed_is_empty);
            tv_verifymobile_invalidcode.setVisibility(View.VISIBLE);
            return;
        } else {
            Object tag = et_verifymobile_code.getTag();
            if (tag != null && tag instanceof String) {
                String tagCode = (String) tag;
                if (!TextUtils.equals(tagCode, code)) {
                    tv_verifymobile_invalidcode.setText(R.string.invalid_code);
                    tv_verifymobile_invalidcode.setVisibility(View.VISIBLE);
                    return;
                } else {
                    Intent intent = new Intent(this, ResetPassActivity.class);
                    intent.putExtra(CommonConstant.RESET_LOGIN_PASS_CODE, tagCode);
                    intent.putExtra(CommonConstant.RESET_LOGIN_PASS_EMAIL, resetLoginPassEmail);
                    startActivity(intent);
                    finish();
                }
            }
        }

    }

    @Override
    protected void initEvent() {
        tv_verifymobile_invalidcode.setBindEditText(et_verifymobile_code);

    }

    @OnClick({
            R.id.bt_verifymobile_verifycode,
            R.id.bt_verifymobile_verifyemail,
            R.id.tv_verifymobile_getcode
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_verifymobile_getcode:
                et_verifymobile_code.setTag("");
                sendCode();
                break;
            case R.id.bt_verifymobile_verifycode:
                verifyCode();
                break;
            case R.id.bt_verifymobile_verifyemail:
                break;
        }

    }

    @Override
    public void sendCodeResult(ResultModel<CodeModel> resultModel, CodePresenter.CodeType codeType) {
        if (resultModel != null && resultModel.isSuccess()) {
            ToastUtil.showToast(getString(R.string.otp_send_success));
            getPresenter().interval();

            CodeModel codeModel = resultModel.getData();
            if (codeModel != null) {
                et_verifymobile_code.setTag(codeModel.getOTP_code());
                if (GlobalValue.isDebug) {
                    et_verifymobile_code.setText(codeModel.getOTP_code());
                }
            }

        }
    }

    @Override
    public void intervalResult(long aLong) {
        Timber.d("--->>>>>>>>---aLong:" + aLong);
        if (aLong == 0) {
            tv_verifymobile_getcode.setEnabled(true);
            tv_verifymobile_getcode.setText(R.string.chongxinhuoquyanzhengma);
        } else {
            tv_verifymobile_getcode.setEnabled(false);
            tv_verifymobile_getcode.setText(aLong + "s");
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onChangeProfileEvent(ChangeProfileEvent event) {
        if (event != null && event.changeProfileType == ChangeProfileType.ChangeLoginPass) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
